package com.bosssoft.boss_exam_web.Controller;

import com.bosssoft.boss_exam_core.exception.UpdateEntityException;
import com.bosssoft.boss_exam_core.util.web.ResponseUtil;
import com.bosssoft.boss_exam_db.entity.Applicant;
import com.bosssoft.boss_exam_db.entity.ApplicantQuestionScore;
import com.bosssoft.boss_exam_db.model.DTO.QuestionAnswerDTO;
import com.bosssoft.boss_exam_db.service.busines.ApplicantQuestionScoreService;
import com.bosssoft.boss_exam_db.service.busines.ApplicantService;
import com.bosssoft.boss_exam_web.common.annotation.LoginApplicant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@Validated
@Slf4j
@Api(value="交卷控制器")
public class SubmitExamPaperController {

//  引入应聘者考试记录服务
    @Autowired
    private ApplicantQuestionScoreService applicantQuestionScoreService;

//  引入应聘者服务
    @Autowired
    private ApplicantService applicantService;

    @ApiOperation(value="提交试卷", notes="手机端提交试卷（无图片上传的提交，暂时不用了，转为com.bosssoft.boss_exam_web.Controller.UploadBase64ImgController）" ,httpMethod="POST",response = Object.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name="name", value="问题答案DTO", dataType = "QuestionAnswerDTO", required=false, dataTypeClass=QuestionAnswerDTO[].class),
    })
    @PostMapping("/phone/examPaper/submitExamPaper")
    public Object submitExamPaper(@LoginApplicant String applicantId, @RequestBody QuestionAnswerDTO[] questionAnswerDTOS){
        if (applicantId == null) {
            return ResponseUtil.unlogin();
        }
        Applicant applicant =applicantService.getApplicantById(applicantId);
        List<ApplicantQuestionScore> applicantQuestionScores=new ArrayList<>();
        if(questionAnswerDTOS!=null&&questionAnswerDTOS.length!=0){
            Arrays.stream(questionAnswerDTOS).forEach(questionAnswerDTO -> {
                ApplicantQuestionScore applicantQuestionScore = new ApplicantQuestionScore();
                BeanUtils.copyProperties(questionAnswerDTO,applicantQuestionScore);
                // 补充提交试卷的应聘者
                applicantQuestionScore.setApplicantId(applicant.getId());
                applicantQuestionScores.add(applicantQuestionScore);
            });
        }
        Boolean result=false;
        try{
           result = applicantQuestionScoreService.insertApplicantQuestionScoreList(applicantQuestionScores);
        }catch (Exception e){
            log.error("【系统错误】 保存科目失败"+e);
            throw  new UpdateEntityException();
        }
        return ResponseUtil.ok(result);
    }


    @ApiOperation(value="PC端提交试卷", notes="PC端提交试卷" ,httpMethod="POST",response = Object.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name="name", value="问题答案DTO", dataType = "QuestionAnswerDTO", required=false, dataTypeClass=QuestionAnswerDTO[].class),
    })
    @PostMapping("/pc/examPaper/submitExamPaper")
    public Object submitPCExamPaper(@LoginApplicant String applicantId, @RequestBody QuestionAnswerDTO[] questionAnswerDTOS){
        if (applicantId == null) {
            return ResponseUtil.unlogin();
        }
        Applicant applicant =applicantService.getApplicantById(applicantId);
        List<ApplicantQuestionScore> applicantQuestionScores=new ArrayList<>();
        if(questionAnswerDTOS!=null&&questionAnswerDTOS.length!=0){
            Arrays.stream(questionAnswerDTOS).forEach(questionAnswerDTO -> {
                ApplicantQuestionScore applicantQuestionScore = new ApplicantQuestionScore();
                BeanUtils.copyProperties(questionAnswerDTO,applicantQuestionScore);
                // 补充提交试卷的应聘者
                applicantQuestionScore.setApplicantId(applicant.getId());
                applicantQuestionScores.add(applicantQuestionScore);
            });
        }
        Boolean result=false;
        try{
            result = applicantQuestionScoreService.insertApplicantQuestionScoreList(applicantQuestionScores);
        }catch (Exception e){
            log.error("【系统错误】 保存科目失败"+e);
            throw  new UpdateEntityException();
        }
        return ResponseUtil.ok(result);
    }
}
